import {
  Body,
  Controller,
  Inject,
  Param,
  Post,
  Put,
  ParseIntPipe,
  HttpException,
  HttpStatus,
} from '@nestjs/common';
import { UserControlService } from './UserControl.service';
import { CheckAdmin } from '@/decorator/index.decorator';
import UserListDto from '@/dto/UserList.dto';
import UserInfoVo from '@/vo/UserInfo.vo';
import type { PageList } from '@/types/index';

@Controller('admin/user')
@CheckAdmin()
export class UserControlController {
  @Inject(UserControlService)
  private readonly userControlService: UserControlService;

  @Post('list')
  getUserList(@Body() data: UserListDto) {
    const { pageNum, pageSize, username, hobby, status } = data;

    const hobbyArr = (hobby && hobby.split('|')) || [];

    const res: PageList<UserInfoVo> = this.userControlService.getUserList(
      pageNum,
      pageSize,
      username,
      hobbyArr,
      status,
    );
    return res;
  }

  @Put('banned/:id')
  async bannedUser(@Param('id', ParseIntPipe) id: number) {
    const res = await this.userControlService.bannedUser(id);
    if (res) throw new HttpException(res, HttpStatus.BAD_REQUEST);
    return '操作成功';
  }
}
